package com.nuanshui.framework.utils;

import com.nuanshui.framework.lang.SysStringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import javax.imageio.ImageIO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

public class CommonUtil {

	private final static List<String> SIMPLE_TYPE = Arrays.asList(
			"java.lang.Integer", "java.lang.Double", "java.lang.Long",
			"java.lang.Character", "java.lang.Boolean", "java.lang.Float",
			"java.lang.Byte", "java.lang.Short");

	private CommonUtil() {
	}

	@SuppressWarnings("rawtypes")
	private static boolean isEmpty(final Object object, final boolean isForce) {
		// 如果实现自定义的借口

		if (object instanceof Empty) {
			Empty empty = (Empty) object;
			return empty.isEmpty();
		}
		// 如果为空
		if (object == null) {
			return true;
		}
		// 如果是数字类型 则为0也表示是空
		if (object instanceof Number && isForce) {
			Number number = (Number) object;
			double num = number.doubleValue();
			if (num == 0) {
				return true;
			}
		}

		// 如果是字符串
		if (object instanceof String) {
			if (isForce) {
				return SysStringUtils.isEmpty(object.toString());
			} else {
				return SysStringUtils.isBlank(object.toString());
			}
		}
		// 如果是数组
		if (object.getClass().isArray() && isForce) {
			int length = Array.getLength(object);
			return length <= 0;
		}

		// 如果是集合

		if (object instanceof Collection && isForce) {
			Collection collection = (Collection) object;
			return collection.size() <= 0;
		}

		// 如果是map
		if (object instanceof Map && isForce) {
			Map map = (Map) object;
			System.out.println(map.isEmpty());
			return map.isEmpty();
		}

		return false;

	}

	public static boolean isEmpty(final Object object) {
		return isEmpty(object, true);

	}

	public static boolean isBlack(final Object object) {
		return isEmpty(object, false);
	}

	public static boolean isNotEmpty(final Object object) {
		return !isEmpty(object);
	}

	public static boolean isNotBlack(final Object object) {
		return !isBlack(object);
	}

	public interface Empty {
		boolean isEmpty();
	}

	private static String getType(final Object object) {
		return object.getClass().toString();
	}

	public static boolean isDate(final Object object) {
		if (object == null) {
			return false;
		}

		return Date.class.isAssignableFrom(object.getClass());

	}

	/**
	 * 判断是否是集合
	 * 
	 * @return
	 * @author 郭浩
	 * @date 2014-11-6
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isCollection(final Object obj) {
		if (obj == null)
			return false;
		Class clazz = null;
		if (obj instanceof Field) {
			Field field = (Field) obj;
			clazz = field.getType();
		} else {
			clazz = obj.getClass();
		}
		return Collection.class.isAssignableFrom(clazz);

	}

	public static boolean isNotCollection(final Object obj) {

		return !isCollection(obj);

	}

	public boolean isSimpleObject(final Object object) {
		if (object == null) {
			return false;
		}
		return SIMPLE_TYPE.contains(getType(object));
	}

	public static boolean isString(final Object object) {
		if (object == null) {
			return false;
		}
		return "java.lang.String".equals(getType(object));
	}

	public static void copyProperties(final Object source, final Object target) {
		try {
			ConvertUtils.register(new DateLocaleConverter(), Date.class);
			BeanUtils.copyProperties(target, source);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: copyProperties
	 * @Description: 属性copy
	 * @author 郭浩
	 * @param @param source 原对象
	 * @param @param target 目的对象
	 * @param @param debarEmpty 是否排除原对象中值为空的属性
	 * @return void
	 * @throws
	 * @version V1.0
	 */
	public static void copyProperties(final Object source, final Object target,
			final boolean debarEmpty) {
		Field fields[] = source.getClass().getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();
			field.isAccessible();
			try {
				Object object = BeanUtils.getProperty(source, name);
				if (isNotEmpty(object)) {
					BeanUtils.setProperty(target, name, object);
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 判断是否都为null
	 * 
	 * @param objs
	 * @return
	 */
	public static boolean allNull(final Object... objs) {
		if (objs == null) {
			return true;
		}
		for (Object obj : objs) {
			if (obj != null) {
				return false;
			}
		}
		return true;

	}

	/**
	 * 大于等于一个null
	 * 
	 * @param objs
	 * @return
	 */
	public static boolean anyNull(final Object... objs) {
		if (objs == null) {
			return true;
		}
		for (Object obj : objs) {
			if (obj == null) {
				return true;
			}
		}
		return false;

	}

	/**
	 * 对list进行随机排序
	 * 
	 * @author 郭浩
	 * @date 2014-10-14
	 */
	public static <T extends Object> List<T> randomSort(final List<T> list) {
		if (list.size() <= 0) {
			return list;
		}
		Collections.sort(list, new Comparator<T>() {
			@Override
			public int compare(final T o1, final T o2) {
				Random random = new Random();
				int i = random.nextInt(10);
				return i - 5;
			}
		});
		return list;

	}
	
	public static  long getVersionNum(String version) {
            String[] ss = version.split("\\.");
            long i64Ver = 0;
            int i32s = 0;
            for(int i=0,j=4; i<ss.length; i++,j--){
                i32s = Integer.parseInt(ss[i]);
                if(i32s>=0 && i32s<=9){
                    i32s = i32s*100;
                }else if(i32s>=10 && i32s<=99){
                    i32s = i32s*10;
                }
                i64Ver += ((long) Math.pow(1000, j))*i32s;
            }
            return i64Ver;
    }
	
	/**
	 * 生成二维码
	 * @param 
	 * @author guohao
	 * @date 2015年8月7日 下午5:35:48
	 */
	public static void generate2DimensionCode(String data,Integer size,String logoUrl,OutputStream os) {
		
		try {
			StringBuilder accessUrl = new StringBuilder("http://api.cli.im/generate/?");
			accessUrl.append("key=UG7h56");
			accessUrl.append("&data="+URLEncoder.encode(data,"utf-8"));
			accessUrl.append("&width="+size);
			accessUrl.append("&level=H");
			accessUrl.append("logo="+accessUrl);
			URL url = new URL(accessUrl.toString());
			ImageIO.write(ImageIO.read(url.openStream()), "jpeg",os);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static void main(String[] args) {
		System.out.println(getVersionNum("2.1.1"));
	}

}
